from functools import reduce

class Positions():
    target_price = 6579
    current_price = 6780
    high_losts = 0  # 这个值应当持久化

    def get_high_losts(self):
        if abs(self.lost_total) > self.__class__.high_losts:
            self.__class__.high_losts = self.lost_total    
        pass

    def __init__(self, positions) -> None:
        self.positions = positions
        self.lens = len(self.positions)
        self.lost_total = self.get_lost_total()
        self.lost_average = self.get_lost_average()
        self.average_price = self.get_average_price()
        self.diff_current_to_target = self.get_diff_current_to_target()
        self.get_high_losts()

    def get_lost_total(self):
        result = reduce(lambda x, current:x + (self.current_price - current.buy) * current.direction, self.positions, 0)
        return result

    def get_lost_average(self):
        result = self.lost_total/self.lens
        return result

    def get_average_price(self):
        total_buy = reduce(lambda x, current:x + current.buy, self.positions, 0)
        return total_buy/self.lens

    def get_diff_current_to_target(self):
        result = self.current_price - self.target_price
        return result
    
    def __str__(self):
        return str(self.positions)

